<UserControl x:Class="WPFGeniusSudoku.SudokuUC"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:pgo="clr-namespace:WPFGeniusSudoku.SudokuControl"
    xmlns:converter="clr-namespace:GeniusSudokuConverter" 
    xmlns:nup="clr-namespace:Microsoft.Samples.KMoore.WPFSamples.NumericUpDownControl"
    Name="uc"
    >
  <UserControl.Resources>
    <Style TargetType="{x:Type GroupBox}">
      <Setter Property="BorderBrush" >
        <Setter.Value>
          <LinearGradientBrush>
            <LinearGradientBrush.GradientStops>
              <GradientStop Offset="0" Color="Yellow" />
              <GradientStop Offset="1" Color="Green" />
            </LinearGradientBrush.GradientStops>
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
    </Style>

    <Style TargetType="{x:Type Expander}">
      <Setter Property="Background" >
        <Setter.Value>
          <LinearGradientBrush Opacity="0.2">
              <GradientStop Offset="0" Color="Yellow" />
              <GradientStop Offset="1" Color="Green" />
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
    </Style>
    
    <Style x:Key="MiniCellStyle" TargetType="{x:Type Button}">
      <Setter Property="FocusVisualStyle" Value="{x:Null}" />
      <Setter Property="Foreground" Value="LightGray" />
      <Setter Property="Button.FontFamily" Value="Gill Sans MT"/>
      <Setter Property="Button.FontSize" Value="20"/>
      <Setter Property="Button.FontWeight" Value="SemiBold"/>
      <Setter Property="Content" Value="" />
      <Setter Property="Margin" Value="1" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
              <Rectangle x:Name="bg" Opacity="1" Fill="{TemplateBinding Background}" StrokeThickness="0.35"/>
              <Rectangle x:Name="GelBackground" Opacity="0" StrokeThickness="0.35"/>
              <ContentPresenter x:Name="GelButtonContent" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}" />
              <Rectangle x:Name="FlaggedBackground" Fill="{StaticResource Croix}" Margin="4,4,4,4" Visibility="Collapsed"/>
            </Grid>
            <ControlTemplate.Triggers>
              <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Rectangle.Fill" TargetName="GelBackground">
                  <Setter.Value>
                    <RadialGradientBrush>
                        <GradientStop Offset="0" Color="#ffcc00" />
                        <GradientStop Offset="1" Color="Transparent" />
                    </RadialGradientBrush>
                  </Setter.Value>
                </Setter>
                <Setter Property="Rectangle.Opacity" TargetName="GelBackground" Value="0.5"/>
              </Trigger>
              <DataTrigger Binding="{Binding IsFlagged}" Value="True">
                <Setter Property="Rectangle.Visibility" Value="Visible" TargetName="FlaggedBackground"/>
              </DataTrigger>
              <DataTrigger Binding="{Binding IsChecked}" Value="True">
                <Setter Property="Foreground" Value="#606060" />
              </DataTrigger>
              <MultiDataTrigger >
                <MultiDataTrigger.Conditions>
                  <Condition Binding="{Binding ElementName='cbHint', Path='IsChecked'}" Value="True"/>
                  <Condition Binding="{Binding IsNumberPossible}" Value="True"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="Foreground" Value="Green">
                </Setter>
              </MultiDataTrigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    <!-- template for big numbers-->
    <Style x:Key="CellStyle" TargetType="{x:Type Button}">
      <Setter Property="FocusVisualStyle" Value="{x:Null}" />
      <Setter Property="Foreground" Value="Black" />
      <Setter Property="Button.FontFamily" Value="Lucida Handwriting"/>
      <Setter Property="Button.FontSize" Value="{Binding ElementName='cbFontSize', Path='Text'}"/>
      <Setter Property="Button.FontWeight" Value="SemiBold"/>
      <Setter Property="Content" Value="" />
      <Setter Property="Margin" Value="1" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
              <Rectangle Opacity="1" Fill="{TemplateBinding Background}" StrokeThickness="0.35"/>
              <Rectangle >
                <Rectangle.Fill>
                  <SolidColorBrush x:Name="test" Color="Transparent"/>
                </Rectangle.Fill>
              </Rectangle>
              <Rectangle x:Name="GelBackground" Opacity="0" StrokeThickness="0.35"/>
              <ContentPresenter x:Name="GelButtonContent" Margin="0,0,0,0" VerticalAlignment="Center" 
                                  HorizontalAlignment="Center" Content="{TemplateBinding Content}" />
            </Grid>
            <ControlTemplate.Triggers>
              <MultiDataTrigger >
                <MultiDataTrigger.Conditions>
                  <Condition Binding="{Binding IsFixed}" Value="True"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="Rectangle.Fill" TargetName="GelBackground">
                  <Setter.Value>
                    <LinearGradientBrush>
                      <GradientStop Offset="0" Color="#ffcc00" />
                      <GradientStop Offset="1.5" Color="Transparent" />
                    </LinearGradientBrush>
                  </Setter.Value>
                </Setter>
                <Setter Property="Rectangle.Opacity" TargetName="GelBackground" Value="0.5"/>
              </MultiDataTrigger>
              <MultiDataTrigger >
                <MultiDataTrigger.Conditions>
                  <Condition Binding="{Binding ElementName='cbShowErrors', Path='IsChecked'}" Value="True"/>
                  <Condition Binding="{Binding IsError}" Value="True"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="Rectangle.Fill" TargetName="GelBackground">
                  <Setter.Value>
                    <LinearGradientBrush>
                      <GradientStop Offset="0" Color="#ff0000" />
                      <GradientStop Offset="1" Color="Transparent" />
                    </LinearGradientBrush>
                  </Setter.Value>
                </Setter>
                <Setter Property="Rectangle.Opacity" TargetName="GelBackground" Value="0.5"/>
              </MultiDataTrigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
      <Setter Property="RenderTransformOrigin" Value="0.5,0.5" />
      <Setter Property="RenderTransform">
        <Setter.Value>
          <TransformGroup>
            <TranslateTransform X="0" Y="0"/>
            <ScaleTransform ScaleX="1" ScaleY="1"/>
            <TranslateTransform X="0" Y="0"/>
          </TransformGroup>
        </Setter.Value>
      </Setter>
      <Style.Triggers>
        <Trigger Property="IsVisible" Value="True">
          <Trigger.EnterActions>
            <BeginStoryboard >
              <Storyboard >
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)">
                  <SplineDoubleKeyFrame KeySpline="0.5,0.5,0.5,0.5" Value="0.085289661252321777" KeyTime="00:00:00"/>
                  <SplineDoubleKeyFrame KeySpline="0.5,0.5,0.5,0.5" Value="-0.19293209245076204" KeyTime="00:00:00.2920000"/>
                  <SplineDoubleKeyFrame KeySpline="0.5,0.5,0.5,0.5" Value="0.47143992735100754" KeyTime="00:00:00.5070000"/>
                  <SplineDoubleKeyFrame KeySpline="0.5,0.5,0.5,0.5" Value="-0.82692307692307687" KeyTime="00:00:00.7550000"/>
                  <SplineDoubleKeyFrame KeySpline="0.5,0.5,0.5,0.5" Value="1" KeyTime="00:00:01"/>
                </DoubleAnimationUsingKeyFrames>
              </Storyboard>
            </BeginStoryboard>
          </Trigger.EnterActions>
        </Trigger>
      </Style.Triggers>
    </Style>

    <!--template of sudokugrid-->
    <ControlTemplate x:Key="MiniCell" TargetType="{x:Type Control}">
      <Button Style="{StaticResource MiniCellStyle}" Content="{Binding Number}"/>
    </ControlTemplate>
    <ControlTemplate x:Key="Cell" TargetType="{x:Type Control}">
      <Button Style="{StaticResource CellStyle}" Content="{Binding Number}"/>
    </ControlTemplate>

    <Storyboard x:Key="OnLoaded">
      <DoubleAnimation BeginTime="00:00:00" Duration="00:00:03" From="0" To="1"  Storyboard.TargetName="boardViewBox" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)">

      </DoubleAnimation>
      <DoubleAnimation BeginTime="00:00:00" Duration="00:00:03" From="0" To="1"  Storyboard.TargetName="boardViewBox" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)">

      </DoubleAnimation>
      <DoubleAnimation BeginTime="00:00:00" Duration="00:00:03" From="0" To="360"  Storyboard.TargetName="boardViewBox" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(RotateTransform.Angle)">

      </DoubleAnimation>
    </Storyboard>
    <Storyboard x:Key="OnWonStoryboard">
      <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="tbWon">
        <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="00:00:00"/>
      </ObjectAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="OnNewGameStoryboard">
      <DoubleAnimation BeginTime="00:00:00" Duration="00:00:01"  To="0" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="tbWon">

      </DoubleAnimation>
      <ObjectAnimationUsingKeyFrames BeginTime="00:00:02" Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="tbWon">
        <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="00:00:00"/>
      </ObjectAnimationUsingKeyFrames>
      <DoubleAnimation BeginTime="00:00:02" Duration="00:00:00"  To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="tbWon">
      </DoubleAnimation>
    </Storyboard>

    <DataTemplate x:Key="RemainingTemplate">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=Number}"
                 FontFamily="Lucida Handwriting" FontSize="30" FontWeight="UltraBold" />
        <TextBlock Text=" : "
                          FontFamily="/Fonts/#Digital" FontSize="20" VerticalAlignment="Center"/>
        <TextBlock Text="{Binding Path=Remaining}"
                  FontFamily="/Fonts/#Digital" FontSize="20" VerticalAlignment="Center"/>

      </StackPanel>
    </DataTemplate>
    <converter:BooleanToVisibilityConverter x:Key="BooleanConverter" />
  </UserControl.Resources>
  <UserControl.Triggers>
    <!-- storyboard executé au chargement-->
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
      <EventTrigger.Actions>
        <BeginStoryboard Storyboard="{DynamicResource OnLoaded}"/>
      </EventTrigger.Actions>
    </EventTrigger>
    <!-- Storyboard execute lors du demarrage du jeu-->
    <EventTrigger RoutedEvent="pgo:SudokuGrid.OnNewGame" SourceName="sudokugrid1">
      <EventTrigger.Actions>
        <BeginStoryboard Storyboard="{DynamicResource OnNewGameStoryboard}"/>
      </EventTrigger.Actions>
    </EventTrigger>
  </UserControl.Triggers>

  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="130"/>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="150"/>
    </Grid.ColumnDefinitions>
    <!-- menu de gauche-->
    <StackPanel  Grid.Column="0" Margin="0,60,0,0">
      <Expander IsExpanded ="True" Header ="Main Menu" Margin="0,0,0,5">
        <StackPanel>
          <Button Click="OnNewGame" Margin="0,0,0,3">New Game</Button>
          <Button Click="OnAbout" Margin="0,0,0,3">About</Button>
          <!--<Button Click="OnGameWon" Margin="0,0,0,3">Won !</Button>-->
        </StackPanel>
      </Expander>
      <Expander IsExpanded ="True" Header ="New Game Settings" Margin="0,0,0,5">
        <GroupBox Header="Difficulty level" Margin="5,3,5,5">
          <StackPanel>
            <RadioButton IsChecked="True" Content="Easy" Name="rbEasy" Margin="0,0,0,3"/>
            <RadioButton Content="Hard" Name="rbHard"/>
          </StackPanel>
        </GroupBox>
      </Expander>
      <Expander IsExpanded ="False" Header ="Options" Margin="0,0,0,5">
        <StackPanel>
          <GroupBox Header="Help" Margin="5,5,5,5">
            <StackPanel>
              <CheckBox Name="cbHint" Content="Hint" Margin="0,5,5,3"/>
              <CheckBox Name="cbShowErrors" Content="Show errors" Margin="0,5,5,3"/>
              <CheckBox Name="cbReminder" Content="Show reminder" Margin="0,5,5,5" />
            </StackPanel>
          </GroupBox>
          <GroupBox Header="Font" Margin="5,5,5,5">
            <StackPanel>
              <TextBlock Text="Size" Margin="0,5,0,0"/>
              <ComboBox Name="cbFontSize">
                <ComboBoxItem IsSelected="True">80</ComboBoxItem>
                <ComboBoxItem >75</ComboBoxItem>
                <ComboBoxItem >70</ComboBoxItem>
                <ComboBoxItem >65</ComboBoxItem>
                <ComboBoxItem >60</ComboBoxItem>
                <ComboBoxItem >55</ComboBoxItem>
                <ComboBoxItem >50</ComboBoxItem>
                <ComboBoxItem >45</ComboBoxItem>
                <ComboBoxItem >40</ComboBoxItem>
              </ComboBox>
           </StackPanel>
          </GroupBox>
          <Button Content="Clear user's notes" Click="OnClearUserNotes"/>
        </StackPanel>
      </Expander>

    </StackPanel>
    <!--partie centrale-->
    <Grid  Grid.Column="1">
      <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="*"/>
      </Grid.RowDefinitions>
      <!-- logo -->
      <StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="0">
        <Grid>
          <TextBlock Grid.Column="0" Grid.Row="0" 
                     FontFamily="Arial Rounded MT Bold" 
                     FontSize="40" 
                     Foreground="Black"
                     Text="Genius Sudoku" Margin="1,1,0,0" >
          </TextBlock>
          <TextBlock Grid.Column="0" Grid.Row="0" FontFamily="Arial Rounded MT Bold" 
                     FontSize="40" Text="Genius Sudoku" >
            <TextBlock.Foreground>
              <LinearGradientBrush StartPoint='0.0110642,0.499497' EndPoint='1,0.499681'>
                <LinearGradientBrush.GradientStops>
                  <GradientStop Offset="0" Color="#C0C0C0"/>
                  <GradientStop Offset="1" Color="sc#1, 0, 0.109317444, 0.3622814"/>
                  <GradientStop Offset="0.48076923076923078" Color="sc#1, 0.08925855, 0.3948507, 0.7072673"/>
                </LinearGradientBrush.GradientStops>
              </LinearGradientBrush>
            </TextBlock.Foreground>
          </TextBlock>
        </Grid>
        <TextBlock Text="WPF version" VerticalAlignment="Bottom" FontSize="8" Margin="0,0,0,5"/>
      </StackPanel>
      <Viewbox x:Name="boardViewBox" Grid.Column="0" Grid.Row="1" 
               HorizontalAlignment="Left" ClipToBounds="False" VerticalAlignment="Top"
               RenderTransformOrigin="0.5,0.5">
        <Viewbox.RenderTransform>
          <TransformGroup>
            <TranslateTransform X="0" Y="0"/>
            <ScaleTransform ScaleX="1" ScaleY="1"/>
            <SkewTransform AngleX="0" AngleY="0"/>
            <RotateTransform Angle="0"/>
            <TranslateTransform X="0" Y="0"/>
          </TransformGroup>
        </Viewbox.RenderTransform>
        <Grid x:Name="gameGrid" Height="900" Width="900" ClipToBounds="False">
          <Rectangle RadiusX="9" RadiusY="9">
            <Rectangle.Fill>
              <ImageBrush ImageSource="BoardBackground.jpg"/>
            </Rectangle.Fill>
          </Rectangle>
          <pgo:SudokuGrid x:Name="sudokugrid1" Background="Transparent" 
                          MiniCellTemplate="{StaticResource MiniCell}" 
                          CellTemplate="{StaticResource Cell}"
                          OnWon="OnGameWon">
          </pgo:SudokuGrid>

          <TextBlock Name="tbWon" Text="Won !" TextAlignment="Center" HorizontalAlignment="Center" 
                     VerticalAlignment="Center"
                     FontFamily="Tahoma" FontSize="120" FontWeight="UltraBold" Foreground="OrangeRed" Visibility="Collapsed">
          </TextBlock>
        </Grid>
      </Viewbox>
    </Grid>
    <StackPanel Grid.Column="2" Margin="0,60,0,0">
      <GroupBox Header="Time" Margin="5,0,5,0">
        <TextBlock Text="{Binding ElementName=sudokugrid1, Path='Time'}" FontFamily="/Fonts/#Digital" FontSize="50"/>
      </GroupBox>
      <GroupBox Name="gbReminder" Header="Reminder" Margin="5,0,5,0" Visibility="{Binding ElementName=cbReminder, Path='IsChecked', Converter={StaticResource BooleanConverter}}">
        <ItemsControl ItemsSource="{Binding ElementName=sudokugrid1, Path='Remainings'}" 
                      ItemTemplate="{StaticResource RemainingTemplate}"
                      />
    </GroupBox>
    </StackPanel>
  </Grid>
</UserControl>
